# coding=utf-8

# input：
# 1.beta问题列表excel：问题链接+版本+SN+问题时间
# 2.9类手机apr问题列表excel：SN+问题时间+问题类型

# excel
import xlrd
from xlutils.copy import copy
# spider
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# Beta问题Excel文件名（或路径）
path_beta = "beta.xls"

chrome_options = Options()
chrome_options.add_argument(r"user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data")
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(executable_path=r"D:\Python\Python37\Scripts\chromedriver.exe", chrome_options=chrome_options)

    
def get_time(beta_url):
    p(beta_url)
    
    try:
        driver.get(beta_url)
        time = driver.find_element_by_id("summary-service").find_element_by_tag_name("b").text
        return time
    except:
        print("error: get_time ")
        return None


def get_time_by_url(rsheet1, wsheet1):
    
    link_map = rsheet1.hyperlink_map
    p("len(link_map)=" + str(len(link_map)))
    
    row_count = rsheet1.nrows
    #     col_count = rsheet1.ncols
    #     p("row_count=" + str(row_count) + " col_count=" + str(col_count))
    
    for row_index in range(1, row_count):
        
        beta_url = link_map.get((row_index, 0)).url_or_path
        #     p(beta_url)
        
        beta_time = get_time(beta_url)
        if beta_time != None:
            wsheet1.write(row_index, 2, beta_time)

    
def p(s):
    if 1:
        print(s)


if __name__ == '__main__':
    
    # 1.beta apr 
    rexcel = xlrd.open_workbook(path_beta, formatting_info=True)
    rsheet1 = rexcel.sheet_by_index(0)

    wexcel = copy(rexcel)
    wsheet1 = wexcel.get_sheet(0)
    
    # 2.begin 
    get_time_by_url(rsheet1, wsheet1)
    
    wexcel.save(path_beta)
    
    # 3.end
    driver.quit()
    print("over")
    
